package tt.love.hw.common;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.MethodUtils;

public class ActionExecutor {
	private Object actionInstance;
	private String method;
	
	

	public ActionExecutor(Object actionInstance, String method) {
		super();
		this.actionInstance = actionInstance;
		this.method = method;
	}

	public Object getActionInstance() {
		return actionInstance;
	}

	public void setActionInstance(Object actionInstance) {
		this.actionInstance = actionInstance;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}
	
	public ViewRender execute(ActionContext actionContext){
		
		try {
			return (ViewRender)MethodUtils.invokeMethod(actionInstance, method, new Object[]{actionContext});
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
			throw new ActionException(e);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			throw new ActionException(e);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			if(e.getTargetException() instanceof ActionException){
				throw (ActionException)e.getTargetException();
			}else {
				throw new ActionException(e);
			}
	
		}
		
		
	}

}
